<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Widgets_Localizer_Widget extends Gio_Core_Widget 
{
	protected function showAction()
	{
		$currRoute  = Gio_Core_Route::getCurrentRoute();
		$routeName 	= $currRoute['name'];
		$defaults 	= $currRoute;
		
		$request = $this->getRequest();
		$this->view->lang = $request->getParam('lang');
		
		$links     = array();
		if (isset($defaults['localization']['enable']) 
			&& ('true' == $defaults['localization']['enable'])
			&& isset($defaults['localization']['identifier']['class']))
		{
			$class  = $defaults['localization']['identifier']['class'];
			$name   = $defaults['localization']['identifier']['param'];
			
			/**
			 * The DAO method used to get the model instance
			 */
			$method = isset($defaults['localization']['identifier']['method'])
						? $defaults['localization']['identifier']['method']
						: 'getById';
			
			$id     = Gio_Core_Request::getInstance()->getParam($name);
			
			$conn = Gio_Db_Connection::getConnection();
			$translationDao = new Modules_Core_Models_Mysql_Translation();
			$translationDao->setConnection($conn);
			$items = $translationDao->getItems($id, $class);
			
			$array       = explode('_', $class);
			$array[] = $array[count($array) - 1];
			$array[count($array) - 2] = 'Mysql'; 	
				
			$daoClass    = implode('_', $array);
			$daoInstance = new $daoClass();
			$daoInstance->setConnection($conn);
			
			if ($items != null) {
				$config    = Gio_Core_Config_Xml::getConfig('localization');
				$languages = $config->languages->details; 
				
				foreach ($items as $item) {
					if ($item['item_id'] == $id) {
						continue;
					}
					$object = $daoInstance->$method($item['item_id']);
					if ($object != null) {						
						$language = $item['language'];
						if (isset($languages[$item['language']])) {
							$info     = explode('|', $languages[$item['language']]);
							$language = $info[1];
						}
						
						$links[] = array(
							'url'   => $this->view->url($routeName, $object),
							'label' => $language,
						);
					}
				}
			}
		}
		
		$this->view->assign('links', $links);
	}
	
	public function configAction()
	{
		$this->view->moduleName = Gio_Core_Widget::getIntance()->getModuleName();
		$this->view->widgetName = Gio_Core_Widget::getIntance()->getWidgetName();
		$this->view->widgetIndex = $this->getParam('widget_index');
	}
}
